package stage
{
	import control.Arrowing;
	import control.MonsterControl;
	
	import data.DataBase;
	
	import events.DamageToMonster;
	import events.FarmClickEvent;
	import events.MonsterDieEvent;
	
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.geom.Point;
	import flash.net.URLLoader;
	import flash.text.TextField;
	import flash.utils.Timer;
	import flash.utils.setTimeout;
	
	import utils.XMLHelper;
	
	import view.Arrow;
	import view.Building;
	import view.Farm;
	import view.Map;
	
	public class Stage_lv1 extends Sprite
	{
		
		public function Stage_lv1()
		{
			//加载xml
			XMLHelper.loadXML("monster4.xml",onloaded)
			
			//加载数据类
			dat = new DataBase();
			
			//添加地图 背景
			map = new Map();
			addChild(map);
			map.url = dat.bg_url;
			
			//添加空地
			for (var i:int = 0; i < dat.farm_arr.length; i++) 
			{
				farm = new Farm(dat.farm_url);
				addChild(farm);
				farm.x = dat.farm_arr[i][0];
				farm.y = dat.farm_arr[i][1];
			}
			
			mainHptext = new TextField();
			mainHptext.text = mainHp.toString();
			addChild(mainHptext);
			
			moneyText = new TextField();
			moneyText.text = money.toString();
			addChild(moneyText);
			moneyText.x = 50
			//监听空地点击事件
			addEventListener(FarmClickEvent.FARM_CLICKED,farmClicked);
			//监听 箭塔攻击怪物事件
			addEventListener(DamageToMonster.DAMAGE_TO_MONSTER,damageToMonster);
			//监听怪物死亡事件
			addEventListener(MonsterDieEvent.MONSTER_DIE_EVENT,removeMonster);
		}
		/**
		 *金币显示文本 
		 */		
		public static var moneyText:TextField;
		/**
		 *金币 
		 */		
		public static var money:uint=500;
		/**
		 *关卡总血量显示文本 
		 */		
		public static var mainHptext:TextField
		/**
		 *管卡总血量，允许怪物到终点的数量 
		 */		
		public static var mainHp:uint=20;
		/**
		 *塔 
		 */		
		private var building:Building;
		/**
		 *数据 
		 */		
		public var dat:DataBase;
		/**
		 *背景地图 
		 */		
		private var map:Map;
		/**
		 *空地 
		 */		
		private var farm:Farm;
		/**
		 *塔数组 
		 */		
		public var buildingArr:Array = [];
		/**
		 * 怪物数组
		 */		
		public var monsterArr:Array = [];
		/**
		 * 怪物数据
		 */		
		private var xml:XMLList;
		/**
		 *删除怪物
		 */		
		protected function removeMonster(event:MonsterDieEvent):void
		{
			var monster:MonsterControl = event.monster as MonsterControl;
			removeChild(monster);
			monster = null
		}
		/**
		 *塔发射的伤害传给怪物 
		 */		
		protected function damageToMonster(event:DamageToMonster):void
		{
			
			var monster:MonsterControl = event.monster as MonsterControl;
			monster.damage=event.damage;
		}
		/**
		 *加载完毕xml 后添加怪物
		 */		
		private function onloaded(data:Object):void
		{
			xml = new XMLList(data);
			
			//添加怪物
			var timer:Timer = new Timer(3000,10);
			timer.addEventListener(TimerEvent.TIMER,addMonster);
			timer.start();
			addMonster(null);
		}		
		
		/**
		 *添加怪物 
		 */		
		protected function addMonster(event:TimerEvent):void
		{
			var monster:MonsterControl = new MonsterControl(dat,monsterArr);
			addChild(monster)
			monsterArr.push(monster);
		}		
		
		
		/**
		 *空地点击事件
		 * 添加箭塔
		 * 添加箭头
		 */		
		protected function farmClicked(event:FarmClickEvent):void
		{
			
			var farm:Farm = event.farm
			addBuilding(farm);
			
			
		}
		/**
		 *添加箭头 
		 * @param farm:Point
		 * 
		 */		
		private function addArrow(farm:Point):void
		{
			var arrow:Arrow = new Arrow();
			addChildAt(arrow,1)
			var arrowing:Arrowing = new Arrowing(arrow,farm,monsterArr);
			
		}
		/**
		 *添加箭塔 
		 * @param farm:Point
		 * 
		 */		
		private function addBuilding(farm:Farm):void
		{
			if(money>=300)
			{
				updateMoney(-300)
				
				building = new Building(dat.building_url);
				addChild(building);
				//设置空地坐标
				building.x = farm.x;
				building.y = farm.y;
				
				
				var point:Point = new Point(farm.x,farm.y);
				addArrow(point);
				
				removeChild(farm);
				farm = null;
				
			}
		}
		public static function updateMainHp(_hp:int):void
		{
			mainHp+=_hp
			mainHptext.text = mainHp.toString();
		}
		public static function updateMoney(_money:int):void
		{
			money+=_money
			moneyText.text = money.toString();
		}
	}
}